package com.systemsjr.jrbbf.expense;

import java.util.Vector;

import net.rim.device.api.database.Cursor;
import net.rim.device.api.database.DataTypeException;
import net.rim.device.api.database.Database;
import net.rim.device.api.database.DatabaseException;
import net.rim.device.api.database.Row;
import net.rim.device.api.database.Statement;

import com.systemsjr.jrbbf.ExpenseUI;
import com.systemsjr.jrbbf.common.FinanceEntity;
import com.systemsjr.jrbbf.db.SQLManager;

public class ManageExpenseType extends SQLManager {

	public ManageExpenseType(Database db) {
		super(db);
	}

	/**
	 * Adds an item to the Items table in the database
	 * 
	 * @param name
	 *            The name of the business represented by the directory item to
	 *            be added
	 * @param location
	 *            The street address of the business represented by the
	 *            directory item to be added
	 * @param phone
	 *            The phone number for the business represented by the directory
	 *            item to be added
	 * @param categoryID
	 *            The category to which the directory item to be added belongs
	 * @return The id of the new directory item
	 */
	public ExpenseType add(ExpenseType entity) {
		try {
			// Insert a new record in the DirectoryItems table
			Statement statement = _db
					.createStatement("INSERT INTO ExpenseType VALUES(null, ?, ?)");
			statement.prepare();
			statement.bind(1, entity.getCode());
			statement.bind(2, entity.getExpenseType());
			statement.execute();
			statement.close();

			// Retrieve the auto-generated ID of the item just added
			entity.setId(_db.lastInsertedRowID());
		} catch (DatabaseException dbe) {
			dbe.printStackTrace();
			ExpenseUI.errorDialog(dbe.toString());
		}

		return entity;
	}

	/**
	 * Updates an existing record in the DirectoryItems table
	 * 
	 * @param id
	 *            The id of the item to update
	 * @param name
	 *            The text with which to update the name field
	 * @param location
	 *            The text with which to update the location field
	 * @param phone
	 *            The text with which to update the phone field
	 */
	public void update(ExpenseType type) {
		try {
			// Update the record in the DirectoryItems table for the given id
			Statement statement = _db
					.createStatement("UPDATE ExpenseType SET code = ?, expenseType = ? WHERE id = ?");
			statement.prepare();
			statement.bind(1, type.getCode());
			statement.bind(2, type.getExpenseType());
			statement.bind(3, type.getId());
			statement.execute();
			statement.close();
		} catch (DatabaseException dbe) {
			// SQLiteDemo.errorDialog(dbe.toString());
		}
	}

	/**
	 * Deletes a directory item record from the database
	 * 
	 * @param id
	 *            The id of the directory item to delete
	 */
	public void delete(FinanceEntity expenseType) {
		try {
			// Delete the record in the expenses
			ManageExpense expenseManager = new ManageExpense(_db);
			expenseManager.delete(expenseType);
			
			//delete the expense type
			Statement statement = _db
					.createStatement("DELETE FROM ExpenseType WHERE id = ?");
			statement.prepare();
			statement.bind(1, expenseType.getId());
			statement.execute();
			statement.close();
		} catch (DatabaseException dbe) {
			// SQLiteDemo.errorDialog(dbe.toString());
		}
	}

	/**
	 * Retrieves all records in the DirectoryItems database table and returns a
	 * vector of DirectoryItem objects.
	 * 
	 * @return A vector of DirectoryItem objects, one for each record in the
	 *         DirectoryItem table
	 */
	public Vector getAll() {
		Vector expenseTypes = new Vector();

		try {
			// Read in all records from the DirectoryItems table
			Statement statement = _db
					.createStatement("SELECT id, code, expense_type FROM ExpenseType");
			statement.prepare();
			Cursor cursor = statement.getCursor();

			// Iterate through the the result set. For each row, add a
			// new DirectoryItem object to the vector.
			while (cursor.next()) {
				Row row = cursor.getRow();

				int id = row.getInteger(0);
				String code = row.getString(1);
				String type = row.getString(2);

				ExpenseType expenseType = new ExpenseType(id, code, type);
				expenseTypes.addElement(expenseType);
			}
			statement.close();
			cursor.close();
		} catch (DatabaseException dbe) {
			// SQLiteDemo.errorDialog(dbe.toString());
		} catch (DataTypeException dte) {
			// SQLiteDemo.errorDialog(dte.toString());
		}

		return expenseTypes;
	}

	/**
     * 
     */
	public ExpenseType get(long id) {
		ExpenseType expenseType = null;
		try {		
			Statement statement = _db
					.createStatement("SELECT id, code, expense_type FROM ExpenseType where id = ?");
			statement.prepare();
			statement.bind(1, id);
			Cursor cursor = statement.getCursor();

			if (cursor.next()) {
				Row row = cursor.getRow();
				expenseType = new ExpenseType(row.getInteger(0),
						row.getString(1), row.getString(2));

			}
		} catch (DatabaseException dbe) {

		} catch (DataTypeException dte) {

		}

		return expenseType;
	}

}
